﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using FreeSql.Internal.Model;
using Masuit.Tools.DateTimeExt;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Bzzb.Controllers
{
    public class DingQiKongZhiXiangController : BaseController
    {
        private readonly ILogger<DingQiKongZhiXiangController> _logger;
        public DingQiKongZhiXiangController(ILogger<DingQiKongZhiXiangController> logger, IFreeSql fsql) : base(fsql)
        {
            this._logger = logger;
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index([FromForm] long bzzbtid, [FromForm] long bzzbid, [FromForm] long jtid, [FromForm] long gjid, [FromForm] int pageIndex = 1, [FromForm] int pageSize = 10)
        {
            List<DingQiKongZhiXiang> dingqikongzhixiangs = await _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Bzzbtid == bzzbtid && dqkzx.Bzzbid == bzzbid && dqkzx.Jtid == jtid && dqkzx.Gjid == gjid).Count(out var total).Page(pageIndex, pageSize).ToListAsync();
            JArray ja_dingqikongzhixiang = new();
            foreach (var dingqikongzhixiang in dingqikongzhixiangs)
            {
                if (dingqikongzhixiang.Gzsj == 0) {
                    dingqikongzhixiang.Gzsj = Convert.ToInt32((DateTime.Now.GetTotalSeconds() - dingqikongzhixiang.Qssj.GetTotalSeconds()) / 60);
                }
                JObject jo_dingqikongzhixiang = JObject.FromObject(dingqikongzhixiang);
                JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                jo_dingqikongzhixiang.Add("jsxobj", jishixiang == null ? null : JObject.FromObject(jishixiang));
                ja_dingqikongzhixiang.Add(jo_dingqikongzhixiang);
            }
            JObject data = new();
            data.Add("total", total);
            data.Add("list", ja_dingqikongzhixiang);
            return RenderJson(data: data);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Save([FromForm] DingQiKongZhiXiang dingqikongzhixiang)
        {
            long id = await _fsql.Insert(dingqikongzhixiang).ExecuteIdentityAsync();
            dingqikongzhixiang.Id = id;

            return RenderJson(data: dingqikongzhixiang);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Delete([FromForm] long id)
        {
            int affrows = await _fsql.Delete<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Id == id).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Update([FromForm] DingQiKongZhiXiang dingqikongzhixiang)
        {
            int affrows = await _fsql.Update<DingQiKongZhiXiang>().SetSourceIgnore(dingqikongzhixiang, colval => colval == null).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Show([FromForm] long id)
        {
            DingQiKongZhiXiang dingqikongzhixiang = await _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Id == id).ToOneAsync();
            return RenderJson(data: dingqikongzhixiang);
        }
    }
}
